import 'package:huxiaodui/api/api.dart';
import 'package:huxiaodui/api/model/result.dart';
import 'package:huxiaodui/api/model/unit_model.dart';
import 'package:huxiaodui/main.dart';
import 'package:huxiaodui/view/pop_pages/unit_info_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class UnitApi {
  /// 单例模式
  static final UnitApi _instance = UnitApi._internal();
  factory UnitApi() {
    return _instance;
  }
  UnitApi._internal();

  Future<void> join(String code)async {
    await Api.request(

      url: UnitUrl.join,
      data: {"code": code},
      success: (res) {

      },
      isToast: true,
    );
  }



  Future<bool> findByCode(String code, {bool isPushReplacement = false}) async {
    bool ret = false;
    await Api.request(
      isToast: true,
      url: UnitUrl.code,
      data: {"code": code},
      success: (Result res) async {
        UnitModel unit = UnitModel.fromJson(res.data);
        if (isPushReplacement) {
          Navigator.pushReplacement(
            navigatorKey.currentContext!,
            MaterialPageRoute(
              builder: (context) => UnitInfoPage(
                unit: unit,
              ),
            ),
          );
        } else {
          Navigator.push(
            navigatorKey.currentContext!,
            MaterialPageRoute(
              builder: (context) => UnitInfoPage(
                unit: unit,
              ),
            ),
          );
        }

        ret = true;
      },
    );

    return ret;
  }
}
